package com.effectiv.gooruda.rest.api.service;

import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

import org.hornetq.api.jms.management.JMSQueueControl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsTemplate;

import com.effectiv.gooruda.rest.api.domain.CloudMessage;
import com.effectiv.gooruda.rest.api.domain.CloudMessages;
import com.effectiv.gooruda.rest.api.exception.MessageOperationException;
import com.effectiv.gooruda.rest.api.exception.QueueOperationException;
import com.effectiv.gooruda.rest.api.operation.MessageOperations;
import com.effectiv.gooruda.security.rest.RESTSecurityUtil;

//@Component
@Slf4j
public class MessageServiceImpl implements MessageOperations {

	@Autowired
	private JmsTemplate template;

	@Autowired
	private HornetQManagementHelper hornetQManagementHelper;

	private final String QNAME_TEMPLATE = "q.";

	// UC : 006
	@Override
	
	public CloudMessages addMessages(String queueName, CloudMessages messages) {

		log.debug("Saving messages - {}", messages);

		for (CloudMessage cm : messages) {
			
			Message m = template.execute(getDestinationName(queueName),
					new DefaultProduerCallback(cm));
			
			try {
				cm.setId(StringUtil.remove(m.getJMSMessageID(), "ID:"));
				
			} catch (JMSException e) {
				log.error("ERROR", e);
			}
		}
		return messages;
	}

	// UC : 007
	@Override
	public CloudMessages findAll(String queueName, int max, boolean delete) {
		SimpleBrowserCallback callback = new SimpleBrowserCallback(max);

		template.browse(getDestinationName(queueName), callback);

		List<Message> jmsMessages = callback.getMessages();

		CloudMessages cqMessages = new CloudMessages();
		JMSQueueControl qControl = null;
		if (delete) {
			qControl = hornetQManagementHelper
					.getJMSQueueControl(getDestinationName(queueName));
		}

		String msgId = null;
		for (Message m : jmsMessages) {
			try {
				 msgId = m.getJMSMessageID();
				log.info("Message id - {}",msgId);
				ObjectMessage o = (ObjectMessage) m;
				CloudMessage cqm = (CloudMessage) o.getObject();
				
				cqm.setId(StringUtil.remove(msgId, "ID:"));
				cqMessages.add(cqm);

				if (delete) {
					qControl.removeMessage(m.getJMSMessageID());
				}

			} catch (Exception e) {
				throw new MessageOperationException("Error processing message",
						e);
			}

		}
		return cqMessages;
	}

	// UC : 008
	@Override
	public boolean delete(String queueName, String messageId) {
		
		JMSQueueControl qControl = hornetQManagementHelper
					.getJMSQueueControl(getDestinationName(queueName));
		
		if(qControl == null) {
			throw new QueueOperationException("Queue with name - " + queueName
					+ " not attached to application ");
		}
		
		try {
			return qControl.removeMessage("ID:"+messageId);
		} catch (Exception e) {
			throw new MessageOperationException("Error removing message", e);
		}
				
		
	}

	protected String getDestinationName(String queueName) {
		return QNAME_TEMPLATE + RESTSecurityUtil.getAppId() + "." + queueName;
	}

}
